package com.sinohantech.schedule.job;

import com.sinohantech.schedule.job.core.executor.ScheduleJobExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by JackyChang on 2018/8/23.
 *
 */
@Configuration
@ConfigurationProperties(prefix = "sinohantech.schedule.job")
public class ScheduleJobAutoConfig implements SmartLifecycle {

    private Logger logger = LoggerFactory.getLogger(ScheduleJobAutoConfig.class);

    private boolean isRunning = false;

    private ScheduleJobExecutor scheduleJobExecutor;

    private String adminAddresses;

    private String appName;

    private String ip;

    private int port;

    private String accessToken;

    private String logPath;

    private int logRetentionDays;

    @Bean(destroyMethod = "destroy")
    public ScheduleJobExecutor scheduleJobExecutor() {
        logger.info(">>>>>>>>>>> schedule-job config init start.");
        ScheduleJobExecutor scheduleJobExecutor = new ScheduleJobExecutor();
        scheduleJobExecutor.setAdminAddresses(adminAddresses);
        scheduleJobExecutor.setAppName(appName);
        scheduleJobExecutor.setIp(ip);
        scheduleJobExecutor.setPort(port);
        scheduleJobExecutor.setAccessToken(accessToken);
        scheduleJobExecutor.setLogPath(logPath);
        scheduleJobExecutor.setLogRetentionDays(logRetentionDays);
        this.scheduleJobExecutor = scheduleJobExecutor;
        logger.info(">>>>>>>>>>> schedule-job config success.");
        return scheduleJobExecutor;
    }


    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable callback) {
        callback.run();
        isRunning = false;
    }

    @Override
    public void start() {
        try {
            scheduleJobExecutor.start();
        } catch (Exception e) {
            logger.info("初始化调度服务失败：" + e.getMessage());
            throw new RuntimeException(e);
        }
        isRunning = true;
    }

    @Override
    public void stop() {
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public int getPhase() {
        return 0;
    }

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public String getAdminAddresses() {
        return adminAddresses;
    }

    public void setAdminAddresses(String adminAddresses) {
        this.adminAddresses = adminAddresses;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }

    public int getLogRetentionDays() {
        return logRetentionDays;
    }

    public void setLogRetentionDays(int logRetentionDays) {
        this.logRetentionDays = logRetentionDays;
    }

}
